package leetcode;

import list.LinkedListCreate;
import list.ListNode;

public class DelDuplicateNodes {

	public static void main(String[] args) {
		DelDuplicateNodes d = new DelDuplicateNodes();
		int[] array = {1,1};
		ListNode head = LinkedListCreate.createList(array);
		head = d.deleteDuplicates(head);
		if(head != null){
			LinkedListCreate.printList(head);
		}else{
			System.out.println("is null");
		}
	}
	public ListNode deleteDuplicates(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		// create a new head, and then we would't change the head pointer
		ListNode fakeHead = new ListNode(0);
		fakeHead.next = head;
		ListNode node = head;
		ListNode pre = fakeHead;
		while (node != null) {
//			System.out.println(node.val);
			while (node.next != null && node.val == node.next.val) {
				node = node.next;
			}
			// 没有移动
			if (pre.next == node) {
				pre = pre.next;
			} else {
				pre.next = node.next;
			}
			node = node.next;
		}
		return fakeHead.next;
	}
}
